package com.example.android.ui.me.user;

import android.os.Bundle;

import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.android.R;
import com.example.android.base.BaseFragment2;
import com.example.android.bean.User;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.FileUpload;
import com.example.android.utils.GlideEngine;
import com.example.android.utils.NetUtil;
import com.example.android.utils.SPUtils;
import com.google.android.material.snackbar.Snackbar;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

public class InfoFragment extends BaseFragment2 {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final User[] currentUser = {new User()};
        View root = inflater.inflate(R.layout.fragment_info, container, false);
        StringBuilder url = new StringBuilder("");

        TextView textView = root.findViewById(R.id.textView);
        TextView textView2 = root.findViewById(R.id.textView2);
        TextView textView3 = root.findViewById(R.id.textView3);
        TextView textView4 = root.findViewById(R.id.textView4);
        TextView textView5 = root.findViewById(R.id.textView5);
        CircleImageView viewById = root.findViewById(R.id.profile_image);
        LinearLayout head_image = root.findViewById(R.id.header_image);
        Button save = root.findViewById(R.id.save);
        head_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PictureSelector.create(getContext())
                        .openGallery(SelectMimeType.ofImage())
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .setMaxSelectNum(1)
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                GlideEngine.createGlideEngine().loadGridImage(getContext(), result.get(0).getPath(), viewById);
                                FileUpload.uploadImageAvatar(result.get(0).getRealPath());
//                                save.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onCancel() {

                            }
                        });

            }
        });


        NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class).getUserInfo().observe(getViewLifecycleOwner(), baseUserModel -> {

            if (baseUserModel.getCode()!=null &&baseUserModel.getCode() == 200) {

                currentUser[0] = baseUserModel.getUser();
                textView.setText(currentUser[0].getUserName());
                textView2.setText(currentUser[0].getNickName());
                textView3.setText(Objects.equals(currentUser[0].getSex(), "1") ? "女" : "男");
                textView4.setText(currentUser[0].getEmail());
                textView5.setText(currentUser[0].getAvatar());
                if (currentUser[0].getAvatar() != null && currentUser[0].getAvatar().length() > 10) {
                    GlideEngine.createGlideEngine().loadGridImage(getContext(), NetUtil.INSTANCE.getBaseUrlDev()+currentUser[0].getAvatar(), viewById);
                }

            }
        });


        Button button = root.findViewById(R.id.button);
        button.setOnClickListener(this::logOut);

        return root;
    }

    private void logOut(View view) {
        SPUtils.putString("token", null);
        Navigation.findNavController(view).navigateUp();
        Snackbar.make(view, "退出登录", Snackbar.LENGTH_LONG).show();
    }
}